

//
//  GWelcomeViewController.swift
//  sinaWeiBoAI
//
//  Created by Z.J on 16/3/22.
//  Copyright © 2016年 Z.J. All rights reserved.
//

import UIKit

class GWelcomeViewController: UIViewController {

    
    var iconViewBottomCons : NSLayoutConstraint?
    // MARK: - view lifeclicy
    override func viewDidLoad() {
        super.viewDidLoad()

   
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        prepareUI()
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 当界面显示完成后调用动画
        moveAnimation()
    }
    
    /// 头像移动的动画
    private func moveAnimation()
    {   // 动画
        // usingSpringWithDamping: 弹簧名明显程度, 0 - 1, 数值越小,弹簧效果越明显
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 改变约束
            self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height-160)
            // 刷新约束 --> 不刷新不起作用
            self.view.layoutIfNeeded()

            }) { (_) -> Void in
               
            UIView.animateWithDuration(GDefaultDuration, animations: { () -> Void in
                self.welcomeLabel.alpha = 1.0
            })
        }
        
    }

    // MARK: - 添加UI
    private func prepareUI()
    {
        // 添加进view
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景bkgImage约束
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像iconView约束
       self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
         iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottomCons!)
        
        // 欢迎label
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    
    
    
    // MARK: - 懒加载
    /// 背景
    private lazy var bkgImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 图像
    private lazy var iconView :UIImageView =
    {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        view.layer.cornerRadius = 42.5
//        view.layer.masksToBounds
        view.clipsToBounds = true

        return view
    }()

    /// 欢迎label
    private lazy var welcomeLabel :UILabel =
    {
        let label = UILabel()
        // 字体大小
        label.font = UIFont.systemFontOfSize(14)
        // 自适应
        label.sizeToFit()
        // 文本内容
        label.text = "欢迎归来"
        // 透明度
        label.alpha = 0
        
        return label
    }()
    
}
